<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2019/1/2
 * Time: 09:41
 */

namespace app\modules\observer;


use SplObserver;

class User implements \SplSubject
{
    private $_email;

    private $_observers;

    /**
     * User constructor.
     */
    public function __construct()
    {
        $this->_observers = new \SplObjectStorage();
    }

    /**
     * className
     *
     * @date   2019/1/2
     * @author edmmss
     * @param SplObserver $observer
     */
    public function attach(SplObserver $observer)
    {
        // TODO: Implement attach() method.
        $this->_observers->attach($observer);
    }

    /**
     * className
     *
     * @date   2019/1/2
     * @author edmmss
     * @param SplObserver $observer
     */
    public function detach(SplObserver $observer)
    {
        // TODO: Implement detach() method.
        $this->_observers->detach($observer);
    }

    /**
     * 通知观察者
     *
     * @date   2019/1/2
     * @author edmmss
     */
    public function notify()
    {
        // TODO: Implement notify() method.
        foreach ($this->_observers as $observer) {
            /* @var SplObserver $observer */
            $observer->update($this);
        }
    }

    /**
     * 修改属性
     *
     * @date   2019/1/2
     * @author edmmss
     * @param string $email
     */
    public function changeEmail(string $email)
    {
        $this->_email = $email;
        $this->notify();
    }
}